package cn.iocoder.yudao.framework.common.util.shortchain;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author liujq
 * @date 20241222 15:46
 * 获取短链工具
 */
public class ShortChainUtil {

    private static final String API_URL = "https://d.888xh.cn/pages/api.php";

    private static final String INSTALL_URL = "https://i.damu8.cn";

    private ShortChainUtil() {
    }

    public static String getShortChain(String longUrl) {

        try {
            // 创建URL对象
            URL url = new URL(API_URL);

            // 打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 设置请求方法
            conn.setRequestMethod("POST");

            // 设置请求头
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            // 允许输出
            conn.setDoOutput(true);

            // 准备请求参数
            String params = "url=" + URLEncoder.encode(longUrl, "UTF-8");

            // 发送请求
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = params.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // 获取响应
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                JSONObject jsonResponse = JSON.parseObject(response.toString());
                return jsonResponse.getString("url");
            }

        } catch (Exception e) {
            throw new RuntimeException("生成短链接失败", e);
        }


    }


    public static String getInstallUrl(String appName,String iconUrl,String bId,String ipaUrl,String urlStr){
        try {
            if(urlStr==null){
                urlStr=INSTALL_URL;
            }
            // 创建URL对象
            URL url = new URL(urlStr+"/short.php");

            // 打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 设置请求方法
            conn.setRequestMethod("POST");

            // 设置请求头
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            // 允许输出
            conn.setDoOutput(true);
            String tk=appName+","+iconUrl+","+bId+","+ipaUrl;

            // 准备请求参数
            String params = "tk=" + URLEncoder.encode(tk, "UTF-8");

            // 发送请求
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = params.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // 获取响应
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                JSONObject jsonResponse = JSON.parseObject(response.toString());
                return jsonResponse.getString("url");
            }

        } catch (Exception e) {
            throw new RuntimeException("生成短链接失败", e);
        }
    }

}
